package com.edee.bigday.filedealer.fileRemoveWeight.quange;

import java.io.File;
import java.io.FileFilter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FileRemoveWeight {
    private static List<String> list = new ArrayList<>();

    public static void main(String[] args) {
        System.out.println("文件整理开始");
        System.out.println("文件类型：" + args[0]);
        System.out.println("基准文件路径：" + args[1]);
        System.out.println("删除文件路径" + args[2]);
        String[] fileTypes = args[0].split(",");
        String[] filePaths = args[1].split(",");
        for (String filePath : filePaths) {
            fileOne(fileTypes, filePath);
        }
        fileOne(fileTypes, args[2]);
        System.out.println("文件去重结束");
    }

    private static void fileOne(String[] fileTypes, String filePath) {
        File file = new File(filePath);
        MyFilter filter = new MyFilter(fileTypes);
        File[] subs = file.listFiles(filter);
        for (int i = 0; i < subs.length; i++) {
            File file1 = subs[i];
            long time = file1.lastModified();//返回文件最后修改时间，是以个long型毫秒数
            String createTime = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date(time));
            String string = createTime + file1.length();
            if (list.contains(string)) {
                file1.delete();
            } else {
                list.add(string);
            }
        }
    }
}

class MyFilter implements FileFilter {
    public String[] fileTypes;

    public MyFilter(String[] fileTypes) {
        this.fileTypes = fileTypes;
    }

    /**
     * 过滤条件:指定文件类型list里面的其中一个
     */
    public boolean accept(File file) {
        for (String fileType : fileTypes) {
            if (file.getName().endsWith(fileType) && file.isFile()) {
                return true;
            }
        }
        return false;
    }
}